package com.example.qundui.learndemo;

import android.app.Application;
import android.net.wifi.WifiConfiguration;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lzy.okgo.OkGo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.L;
import com.squareup.picasso.Picasso;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.logging.Level;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;

import org.xutils.x;

/**
 * Created by qundui on 2017/4/22.
 */

public class Myapp extends Application {
    private  static Application _instance;

    @Override
    public void onCreate() {
        super.onCreate();
        _instance=this;
 OkHttpClient client = new OkHttpClient.Builder()
                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                .build();
        Picasso.setSingletonInstance(new Picasso.Builder(this).
                downloader(new ImageDownLoader(client)).loggingEnabled(true)
                .build());
        initImageload();
        Fresco.initialize(this);
        NoHttp.initialize(this);
        Logger.setDebug(BuildConfig.DEBUG);// 开启NoHttp的调试模式, 配置后可看到请求过程、日志和错误信息。
        Logger.setTag("NoHttpSample");// 设置NoHttp打印Log的tag。
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
//        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//                //其他配置
//                .build();
//        OkHttpUtils.initClient(okHttpClient);
        //zhyokhttps();
        initOKgo();
    }

    private void initOKgo() {
        OkGo.init(this);
         OkGo.getInstance().debug("OkGo", Level.INFO,true);
//     OkGo.getInstance().setCertificates();
        try {
           OkGo.getInstance().setCertificates(getAssets().open("bmc.huochexia.com.cer"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void zhyokhttps() {
        InputStream ins =null;
        try {
            ins= getAssets().open("srca.cer");
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{ins}, null, null);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)

        .build();
        OkHttpUtils.initClient(okHttpClient);
//        OkHttpClientManager.getInstance()
//                .setCertificates(getAssets().open("srca.cer"));
    }

    private void initImageload() {
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(this);

        //Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);
        L.enableLogging();
    }
    public static Application getInstance() {
        return _instance;
    }
}
